//
//  LoginVC.swift
//  JTYP
//
//  Created by Groot on 2017/12/5.
//  Copyright © 2017年 inspur. All rights reserved.
//

import Alamofire

class LoginVC: TransparentNavBarVC {
  
    @IBOutlet weak var usernameTv: UITextField!
    @IBOutlet weak var passwordTv: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        usernameTv.attributedPlaceholder = NSAttributedString.init(string:"请输入手机号", attributes: [NSAttributedStringKey.foregroundColor:UIColor.white.withAlphaComponent(0.7)])
        
        passwordTv.attributedPlaceholder = NSAttributedString.init(string:"请输入密码", attributes: [NSAttributedStringKey.foregroundColor:UIColor.white.withAlphaComponent(0.7)])
        //usernameTv.attributedPlaceholder =
    }
    
    @IBAction func login(_ sender: UIButton) {
        guard let param = check() else {
            return
        }
//
        let hud = ShowProgressHUD(view)
        CUserManager.shareInstance.getToken(account: param["account"] as! String, pwd: param["password"] as! String) { [unowned self](user, err) in
            hud.hide(animated: true)
            guard let _ = user else {
                self.view.makeToast( err ?? "登录失败")
                return
            }
            (UIApplication.shared.delegate as? AppDelegate)?.changeStoryboard()
        }
    }
//
    func check() -> Parameters?{

        var param = Parameters();

        if let username = usernameTv.text {
            guard !username.isEmpty else {
                self.view.makeToast(usernameTv.placeholder)
                usernameTv.becomeFirstResponder()
                return nil
            }
            param["account"] = username
        }

        if let password = passwordTv.text {
            guard !password.isEmpty else {
                self.view.makeToast(passwordTv.placeholder)
                passwordTv.becomeFirstResponder()
                return nil
            }
            param["password"] = password
        }
        param["deviceid"] = "1"

        return param
    }
//
    func cancel() {
        navigationController?.dismiss(animated: true, completion: nil)
    }
}
